package licode.unisop.provider.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义资源信息
 *
 * @author licode
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
public class SiAssetHold {
    /**
     * 资源持有者
     */
    private String holderId;

    /**
     * 租户ID
     */
    private String tenantId;

    /**
     * 平台ID
     */
    private String platformId;

    /**
     * 根据ID资源集合
     */
    private Map<String, SiAssetBO> ids;

    /**
     * 具有树形关系的资源集合
     */
    private Map<String, List<SiAssetBO>> trees;

    /**
     * 类型和code资源集合
     */
    private Map<String, Map<String, String>> codes;

    public boolean hasTrees() {
        return (null != trees && !trees.isEmpty());
    }
    public boolean hasIdAssets() {
        return (null != ids && !ids.isEmpty());
    }
    public boolean hasCodeTrees() {
        return (null != codes && !codes.isEmpty());
    }

    public void addTreeAsset(SiAssetBO asset) {
        List<SiAssetBO> assets;
        if (null == trees) {
            trees = new HashMap<>();
        }
        assets = trees.computeIfAbsent(asset.getType(), k -> new ArrayList<>());
        assets.add(asset);
    }

    public void addIdAsset(String id, SiAssetBO asset) {
        if (null == ids) {
            ids = new HashMap<>();
        }
        ids.put(id, asset);
    }

    public void addCodeAsset(String assetType, String code, String assetId) {
        Map<String, String> assetCode;

        if (null == codes) {
            codes = new HashMap<>();
        }

        assetCode = codes.computeIfAbsent(assetType, k -> new HashMap<>());
        assetCode.put(code, assetId);
    }

    public String findAssetByCode(String assetType, String code) {
        Map<String, String> assetCode;
        assetCode = null == codes ? null : codes.get(assetType);
        return null == assetCode ? null : assetCode.get(code);
    }
}
